package com.dwork.base.pagination;
import java.io.Writer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
 
public class Paginator extends SimpleTagSupport {
    private String uri;
    private int currentPage;
    private int totalCount;
    private int totalPage;
    private int maxLinks = 10;
 
    private Writer getWriter() {
        JspWriter out = getJspContext().getOut();
        return out;
    }
 
    @Override
    public void doTag() throws JspException {
        Writer out = getWriter();
 
        boolean lastPage = (currentPage == totalPage) || totalPage==0;
        int pgStart = Math.max(currentPage - maxLinks / 2, 1);
        int pgEnd = pgStart + maxLinks;
        if (pgEnd > totalPage + 1) {
            int diff = pgEnd - totalPage;
            pgStart -= diff - 1;
            if (pgStart < 1)
                pgStart = 1;
            pgEnd = totalPage + 1;
        }
 
        try {
            out.write("<ul class=\"pagination\">");
            
            if(currentPage > 1){
            	out.write(constructLink(currentPage - 1, "&laquo;", null));
            }else{
            	out.write("<li class=\"disabled\"><a>&laquo</a></li>");
            }
            
 
            for (int i = pgStart; i < pgEnd; i++) {
                if (i == currentPage)
                    out.write("<li class=\"active\"><a>"+ currentPage + "</a></li>");
                else
                    out.write(constructLink(i));
            }
            
            if(!lastPage){
            	out.write(constructLink(currentPage + 1, "&raquo;", null));
            }else{
            	out.write("<li class=\"disabled\"><a>&raquo;</a></li>");
            }
 
            out.write("<li class=\"page-total\">共" + totalCount + "条记录</li>");
            out.write("</ul>");
            
        } catch (java.io.IOException ex) {
            throw new JspException("Error in Paginator tag", ex);
        }
    }
 
    private String constructLink(int page) {
        return constructLink(page, String.valueOf(page), null);
    }
 
    private String constructLink(int page, String text, String className) {
        StringBuilder link = new StringBuilder("<li");
        if (className != null) {
            link.append(" class=\"");
            link.append(className);
            link.append("\"");
        }
        link.append(">")
            .append("<a href=\"")
            .append(uri.replace("##", String.valueOf(page)))
            .append("\">")
            .append(text)
            .append("</a></li>");
        return link.toString();
    }
 
    public void setUri(String uri) {
        this.uri = uri;
    }
 
    public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public void setMaxLinks(int maxLinks) {
        this.maxLinks = maxLinks;
    }
}